﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using VirtualShare.Client;

namespace VirtualShare.Extensions
{
    public class ExtensionsManager
    {
        public static List<IExtension> extensions = new List<IExtension>();

        public static IExtension LoadExtension(string path)
        {
            Assembly extAssembly;
            Type extClass;

            try
            {
                extAssembly = Assembly.LoadFile(path);
                extClass = extAssembly.GetTypes().First(t => t.Namespace == "VirtualShare.Extensions");
            }
            catch
            {
                return null;
            }

            ConstructorInfo constructor = extClass.GetConstructor(Type.EmptyTypes);
            IExtension extension = constructor.Invoke(new object[] {}) as IExtension;

            if (extension != null)
                extensions.Add(extension);
            return extension;
        }
    }

    public interface IExtension
    {
        void OnLogin(ref VirtualShare.Client.Client client);
        void OnClientConnection(ref VirtualShare.Client.Client client);
        void OnClientDeconnection(ref VirtualShare.Client.Client client);
    }
}
